package edu.gxnu.cetcdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gxnu.cetcdemo.mapper.PropMapper;
import edu.gxnu.cetcdemo.pojo.Prop;
import edu.gxnu.cetcdemo.service.PropService;
import edu.gxnu.cetcdemo.utils.Result;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author Uplifted
* @description 针对表【prop】的数据库操作Service实现
*` @createDate 2024-04-02 15:09:11
*/
@Service
public class PropServiceImpl extends ServiceImpl<PropMapper, Prop>
    implements PropService{
    @Autowired
    private PropMapper propMapper;
    private static final int PAGE_SIZE = 9;

    @Override
    public Result search(Map<String,Object> map, int pageNumber) {
        String state = (String) map.get("propState");
        String room = (String) map.get("propRoom");
        String period = (String) map.get("propPeriod");
        String type = (String) map.get("propType");
        long time = Long.valueOf(map.get("propDate").toString());

        System.out.println(state + " " + room + " " + period + " " + type + " " + time);

        Page<Prop> page = new Page<>(pageNumber, PAGE_SIZE);
        Page<Prop> propPage;
        LambdaQueryWrapper<Prop> qw = new LambdaQueryWrapper<>();

//        Date date = null;
        long pageCount  = 0;
        long total = 0;
        //构造查询条件，日期查询未调试正确。
        if (state != "") qw = qw.eq(Prop::getPropState, state);
        if (room  != "") qw = qw.eq(Prop::getPropRoom,room);
        if (type  != "") qw = qw.eq(Prop::getPropType,type);
        if (period!= "") qw = qw.eq(Prop::getPropPeriod,period);
        if (time  != 0) qw = qw
                .ge(Prop::getPropDate,new Timestamp(time))
                .lt(Prop::getPropDate, new Timestamp(time + 1000 * 3600 * 24));
        //System.out.println(qw.getSqlSelect());
        propPage = propMapper.selectPage(page, qw);
//        List<Prop> propList = propMapper.selectList(qw);
        List<Prop> propList = propPage.getRecords();
        total = propPage.getTotal();


        //构建返回数据
        Map data = new HashMap();
        data.put("propList",propList);
        data.put("total",total);
        return new Result().build(200,"success",data);
    }

    @Override
    public Result updateProp(Prop prop) {
        this.updateById(prop);
        return new Result<>().build(200,"更新成功");
    }

    //获取页面数量
    private long getPageCount(Page<Prop> page){
        long recordCount = page.getTotal();
        long pageCount = recordCount % PAGE_SIZE == 0 ? recordCount / PAGE_SIZE : recordCount / PAGE_SIZE + 1;
        return pageCount;
    }
}




